package com.dlyk.service.impl;

import com.dlyk.constant.Constants;
import com.dlyk.mapper.ActivityMapper;
import com.dlyk.model.Activity;
import com.dlyk.model.User;
import com.dlyk.query.ActivityQuery;
import com.dlyk.query.BaseQuery;
import com.dlyk.service.ActivityService;
import com.dlyk.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private ActivityMapper activityMapper;

    /**
     * 查询市场活动（PageHelper 分页查询）
     */
    @Override
    public PageInfo<Activity> getActivityListByPage(Integer current, ActivityQuery activityQuery) {

        // 1、设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        // 2、查询（传入一个ActivityQuery类，用于条件查询，ActivityQuery类继承了BaseQuery类，同样拥有filterSQL属性，可以用于拼接过滤SQL）
        List<Activity> activityList = activityMapper.selectActivityListByPage(activityQuery);

        // 3、封装分页数据到PageInfo
        PageInfo<Activity> activityPageInfo = new PageInfo<>(activityList);

        return activityPageInfo;
    }

    /**
     * 录入市场活动
     */
    @Override
    public int saveActivity(ActivityQuery activityQuery, Authentication authentication) {

        Activity activity = new Activity();

        // 使用BeanUtils工具将前端传递的参数转成Activity类型
        BeanUtils.copyProperties(activityQuery, activity);

        // 创建者就是当前登录的用户，通过认证信息获取当前登录用户
        User loginUser = (User) authentication.getPrincipal();

        // 设置创建者
        activity.setCreateBy(loginUser.getId());
        // 设置创建时间
        activity.setCreateTime(new Date());

        return activityMapper.insertSelective(activity);
    }

    /**
     * 获取市场活动信息
     */
    @Override
    public Activity getActivityById(Integer id) {
        return activityMapper.selectByPrimaryKey(id);
    }

    /**
     * 编辑市场活动
     */
    @Override
    public int editActivity(ActivityQuery activityQuery, String token) {

        Activity activity = new Activity();

        // 使用BeanUtils工具将ActivityQuery类转成Activity类
        // 注意：这两个类的属性名和属性类型要保持一致
        BeanUtils.copyProperties(activityQuery, activity);

        // 通过token获取当前登录用户
        User loginUser = JWTUtils.parseUserFromJWT(token);

        // 设置编辑者
        activity.setEditBy(loginUser.getId());
        // 设置编辑时间
        activity.setEditTime(new Date());

        return activityMapper.updateByPrimaryKeySelective(activity);
    }

    /**
     * 根据id查询市场活动详情
     */
    @Override
    public Activity getActivityDetailById(Integer id) {
        return activityMapper.selectActivityDetailById(id);
    }

    /**
     * 删除市场活动
     */
    @Override
    public int removeActivityById(Integer id) {
        return activityMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除市场活动
     */
    @Override
    public int batchRemoveActivity(List<String> ids) {
        return activityMapper.deleteByIds(ids);
    }

    /**
     * 查询所有正在进行的市场活动
     */
    @Override
    public List<Activity> getAllOngoingActivity() {
        return activityMapper.selectAllOngoingActivity();
    }

}

